// import {
// 	RenderingEngine,
// 	getRenderingEngine,
// 	Enums,
// 	utilities as csUtils
// } from "@cornerstonejs/core";
import { vec3 } from "gl-matrix";
import { utilities } from "@cornerstonejs/tools";
const { getOrientationStringLPS, invertOrientationStringLPS } =
	utilities.orientation;

export default function getOrientationMarkers({ image, camera, rotation }) {
	const { ip_x, ip_y, ip_z } = image;
	// console.log("getOrientationMarkers", ip_x, ip_y, ip_z);
	if (!ip_x && !ip_y && !ip_z) {
		return {
			top: "",
			bottom: "",
			left: "",
			right: ""
		};
	}

	// const { rotation, previousCamera, camera } = detail;
	let flipVertical = camera.flipVertical || false;
	let flipHorizontal = camera.flipHorizontal || false;
	let newRotation = rotation || 0;

	let rowCosines, columnCosines;

	// const { viewUp, viewPlaneNormal } = viewport.getCamera();
	const { viewUp, viewPlaneNormal } = camera;

	const viewRight = vec3.create();
	vec3.cross(viewRight, viewUp, viewPlaneNormal);

	columnCosines = [-viewUp[0], -viewUp[1], -viewUp[2]];
	rowCosines = viewRight;

	const rowString = getOrientationStringLPS(rowCosines);
	const columnString = getOrientationStringLPS(columnCosines);
	const oppositeRowString = invertOrientationStringLPS(rowString);
	const oppositeColumnString = invertOrientationStringLPS(columnString);

	const markers = {
		top: oppositeColumnString,
		left: oppositeRowString,
		right: rowString,
		bottom: columnString
	};

	// If any vertical or horizontal flips are applied, change the orientation strings ahead of
	// the rotation applications
	if (flipVertical) {
		markers.top = invertOrientationStringLPS(markers.top);
		markers.bottom = invertOrientationStringLPS(markers.bottom);
	}

	if (flipHorizontal) {
		markers.left = invertOrientationStringLPS(markers.left);
		markers.right = invertOrientationStringLPS(markers.right);
	}

	// Swap the labels accordingly if the viewport has been rotated
	// This could be done in a more complex way for intermediate rotation values (e.g. 45 degrees)
	if (newRotation === 90 || newRotation === -270) {
		return {
			top: markers.left,
			left: invertOrientationStringLPS(markers.top),
			right: invertOrientationStringLPS(markers.bottom),
			bottom: markers.right // left
		};
	} else if (newRotation === -90 || newRotation === 270) {
		return {
			top: invertOrientationStringLPS(markers.left),
			left: markers.top,
			bottom: markers.left,
			right: markers.bottom
		};
	} else if (newRotation === 180 || newRotation === -180) {
		return {
			top: invertOrientationStringLPS(markers.top),
			left: invertOrientationStringLPS(markers.left),
			bottom: invertOrientationStringLPS(markers.bottom),
			right: invertOrientationStringLPS(markers.right)
		};
	}

	return markers;
}
